package cn.dianhun.om.jpa.controller;

import cn.dianhun.om.jpa.pojo.SysRoleEntity;
import cn.dianhun.om.jpa.pojo.SysUserEntity;
import cn.dianhun.om.jpa.repository.RoleRepository;
import cn.dianhun.om.jpa.repository.UserRepository;
import cn.dianhun.om.jpa.repository.UserRoleRepository;
import cn.dianhun.om.jpa.utills.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cyt
 * @create 2020-07-09 9:36
 */
@RestController
public class testManyToMany {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private UserRoleRepository userRoleRepository;

    @GetMapping("/manyTomany")
    public R manyTomany() {
        SysUserEntity userEntity = new SysUserEntity();
        userEntity.setUsername("测试2").setEmail("1721310909@qq.com");

        SysUserEntity userEntity2 = new SysUserEntity();
        userEntity2.setUsername("测试3").setEmail("1721310909@qq.com");


        SysRoleEntity roleEntity2 = new SysRoleEntity();
        roleEntity2.setRoleName("管理1").setRemark("测试");

        SysRoleEntity roleEntity = new SysRoleEntity();
        roleEntity.setRoleName("管理2").setRemark("测试");
        // 设置关系
        userEntity.getRoleEntities().add(roleEntity);
        userEntity.getRoleEntities().add(roleEntity2);

        roleEntity.getUserEntitySet().add(userEntity2);
        roleEntity.getUserEntitySet().add(userEntity);

        userRepository.save(userEntity);
        roleRepository.save(roleEntity);

        return R.ok();
    }

    @ApiOperation("角色放弃主键维护")
    @GetMapping("/manyTomany2")
    public R manyTomany2() {
        SysUserEntity userEntity = new SysUserEntity();
        userEntity.setUsername("测试").setEmail("1721310909@qq.com");

        SysRoleEntity roleEntity = new SysRoleEntity();
        roleEntity.setRoleName("管理").setRemark("测试");

        // 设置关系
        userEntity.getRoleEntities().add(roleEntity);

        roleEntity.getUserEntitySet().add(userEntity);

        userRepository.save(userEntity);
        roleRepository.save(roleEntity);
        return R.ok();
    }

    @ApiOperation("级联添加操作")
    @GetMapping("/manyTomany3")
    public R manyTomany3() {
        SysUserEntity userEntity = new SysUserEntity();
        userEntity.setUsername("测试1").setEmail("1721310909@qq.com");

        SysRoleEntity roleEntity = new SysRoleEntity();
        roleEntity.setRoleName("管理").setRemark("测试");

        // 设置关系
        userEntity.getRoleEntities().add(roleEntity);

        roleEntity.getUserEntitySet().add(userEntity);

        roleRepository.save(roleEntity);

        return R.ok();
    }


    @ApiOperation("级联删除操作")
    @GetMapping("/manyTomany4")
    public R manyTomany4() {
        //1、 查出 角色
        SysRoleEntity one = roleRepository.getOne(9L);
        //2、 删除角色
        roleRepository.delete(one);
        return R.ok();
    }
}
